//
//  WXHelper.m
//  ShareTest
//
//  Created by Eric Wang on 15/2/1.
//  Copyright (c) 2015年 LinkMob. All rights reserved.
//

#import "WXHelper.h"
#import "UIImage+UIImageExt.h"
#import "SDWebImageDownloader.h"
#import "UIImageView+WebCache.h"
#import "SDWebImagePrefetcher.h"

@implementation WXHelper

+ (instancetype)shareInstance {
    
    static dispatch_once_t once;
    static WXHelper *instance = nil;
    dispatch_once(&once, ^{
        instance = [[self alloc] init];
    });
    
    return instance;
}

- (instancetype)init {
    
    self = [super init];
    if (self) {
        
    }
    
    return self;
}

- (void)shareTitle:(NSString *)title
              text:(NSString *)text
        thumbImage:(NSString *)thumbImage
         imageData:(NSData *)imageData
          imageURL:(NSString *)imageURL
              desc:(NSString *)desc
        webPageURL:(NSString *)webPageURL
          mediaURL:(NSString *)mediaURL
      audioDataURL:(NSString *)audioDataURL
             scene:(int)scene
              type:(WXShareType)type {
    
    if (type == WXShareTypeText) {
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        req.text = text;
        req.bText = YES;
        req.scene = scene;
        
        [WXApi sendReq:req];
    }
    else if (type == WXShareTypeImage) {
        WXMediaMessage *message = [WXMediaMessage message];
        //[message setThumbImage:[UIImage imageNamed:@"res1thumb.png"]];
        
        WXImageObject *ext = [WXImageObject object];
        if (imageData) {
            ext.imageData = imageData;
        }
        else {
            //ext. = imageURL;
        }
        
        message.description = desc;
        message.mediaObject = ext;
        message.mediaTagName = @"WECHAT_TAG_JUMP_APP";
        message.messageExt = @"这是第三方带的测试字段";
        message.messageAction = @"<action>dotalist</action>";
        
        SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
        req.bText = NO;
        req.message = message;
        req.scene = scene;
        
        [WXApi sendReq:req];
    }
    else if (type == WXShareTypeLink) {
        
        if ([[SDWebImageManager sharedManager] cachedImageExistsForURL:[NSURL URLWithString:thumbImage]]) {
            [[SDWebImageManager sharedManager] cachedImageExistsForURL:[NSURL URLWithString:thumbImage] completion:^(BOOL isInCache) {
                NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:[NSURL URLWithString:thumbImage]];
                UIImage *image = [[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey:key];
                UIImage *thumbImg = [image imageByScalingAndCroppingForSize:CGSizeMake(300, 300)];
                WXMediaMessage *message = [WXMediaMessage message];
                message.title = title;
                message.description = desc;
                [message setThumbImage:thumbImg];
                
                WXWebpageObject *ext = [WXWebpageObject object];
                ext.webpageUrl = webPageURL;
                
                message.mediaObject = ext;
                message.mediaTagName = @"WECHAT_TAG_JUMP_SHOWRANK";
                
                SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
                req.bText = NO;
                req.message = message;
                req.scene = scene;
                [WXApi sendReq:req];
            }];
        }
        else {
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:thumbImage] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                UIImage *thumbImg = [image imageByScalingAndCroppingForSize:CGSizeMake(300, 300)];
                WXMediaMessage *message = [WXMediaMessage message];
                message.title = title;
                message.description = desc;
                [message setThumbImage:thumbImg];
                
                WXWebpageObject *ext = [WXWebpageObject object];
                ext.webpageUrl = webPageURL;
                
                message.mediaObject = ext;
                message.mediaTagName = @"WECHAT_TAG_JUMP_SHOWRANK";
                
                SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
                req.bText = NO;
                req.message = message;
                req.scene = scene;
                [WXApi sendReq:req];
            }];
        }
    }
    else if (type == WXShareTypeAudio) {
        if ([[SDWebImageManager sharedManager] cachedImageExistsForURL:[NSURL URLWithString:thumbImage]]) {
            [[SDWebImageManager sharedManager] cachedImageExistsForURL:[NSURL URLWithString:thumbImage] completion:^(BOOL isInCache) {
                WXMediaMessage *message = [WXMediaMessage message];
                message.title = title;
                message.description = desc;
                
                NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:[NSURL URLWithString:thumbImage]];
                UIImage *image = [[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey:key];
                UIImage *thumb = [image imageByScalingAndCroppingForSize:CGSizeMake(300, 300)];
                [message setThumbImage:thumb];
                WXMusicObject *ext = [WXMusicObject object];
                ext.musicUrl = mediaURL;
                ext.musicDataUrl = audioDataURL;
                
                message.mediaObject = ext;
                
                SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
                req.bText = NO;
                req.message = message;
                req.scene = scene;
                
                [WXApi sendReq:req];
            }];
        }
        else {
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:thumbImage] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                UIImage *thumbImg = [image imageByScalingAndCroppingForSize:CGSizeMake(300, 300)];
                WXMediaMessage *message = [WXMediaMessage message];
                message.title = title;
                message.description = desc;
                [message setThumbImage:thumbImg];
                
                WXWebpageObject *ext = [WXWebpageObject object];
                ext.webpageUrl = webPageURL;
                
                message.mediaObject = ext;
                message.mediaTagName = @"WECHAT_TAG_JUMP_SHOWRANK";
                
                SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
                req.bText = NO;
                req.message = message;
                req.scene = scene;
                [WXApi sendReq:req];
            }];
        }
    }
    else {
        if ([[SDWebImageManager sharedManager] cachedImageExistsForURL:[NSURL URLWithString:thumbImage]]) {
            [[SDWebImageManager sharedManager] cachedImageExistsForURL:[NSURL URLWithString:thumbImage] completion:^(BOOL isInCache) {
                WXMediaMessage *message = [WXMediaMessage message];
                message.title = title;
                message.description = desc;
                
                NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:[NSURL URLWithString:thumbImage]];
                UIImage *image = [[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey:key];
                UIImage *thumb = [image imageByScalingAndCroppingForSize:CGSizeMake(300, 300)];
                [message setThumbImage:thumb];
                
                WXVideoObject *ext = [WXVideoObject object];
                ext.videoUrl = @"http://v.youku.com/v_show/id_XNTUxNDY1NDY4.html";
                
                message.mediaObject = ext;
                
                SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
                req.bText = NO;
                req.message = message;
                req.scene = scene;
                
                [WXApi sendReq:req];
            }];
        }
        else {
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:thumbImage] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                UIImage *thumbImg = [image imageByScalingAndCroppingForSize:CGSizeMake(300, 300)];
                WXMediaMessage *message = [WXMediaMessage message];
                message.title = title;
                message.description = desc;
                [message setThumbImage:thumbImg];
                
                WXWebpageObject *ext = [WXWebpageObject object];
                ext.webpageUrl = webPageURL;
                
                message.mediaObject = ext;
                message.mediaTagName = @"WECHAT_TAG_JUMP_SHOWRANK";
                
                SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
                req.bText = NO;
                req.message = message;
                req.scene = scene;
                [WXApi sendReq:req];
            }];
        }
    }
}


-(void) onResp:(BaseResp*)resp {
    
    NSLog(@"RE %d", resp.errCode);
}

@end
